package com.lc.user.express.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetChargeInfo;
import com.lc.user.express.httpserver.GetDeleteBankCard;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.utils.PublicMethod;
import com.lc.user.express.utils.image.MD5;
import com.lc.user.express.view.PayParswordPopupWindow;
import com.lc.user.express.view.dialog.AffirmDialog;
import com.zcx.helper.http.AsyCallBack;

import java.util.List;

/**
 * Created by Administrator on 2016/3/14. 充值
 */
public class RechargeActivity extends BaseSecondActivity {
    public static OnRefresh onRefresh = null;
    private LinearLayout ll_set_bank;
    private LinearLayout ll_bank;
    private TextView tv_bankname, tv_number;
    private PayParswordPopupWindow popup;
    private EditText et_money;
    private String num = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        setTitle("充值");
        initView();
        chargeInfo();
    }

    private void initView() {
        ((TextView) findViewById(R.id.tv_public)).setOnClickListener(this);
        ll_set_bank = (LinearLayout) findViewById(R.id.ll_set_bank);
        ll_set_bank.setOnClickListener(this);

        ll_bank = (LinearLayout) findViewById(R.id.ll_bank);
        tv_bankname = (TextView) findViewById(R.id.tv_bankname);
        tv_number = (TextView) findViewById(R.id.tv_number);
        et_money = (EditText) findViewById(R.id.et_money);

        onRefresh = new OnRefresh() {
            @Override
            public void onRefresh() {
                chargeInfo();
            }
        };
        ll_bank.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new AffirmDialog(cnt, "确定要删除该银行卡吗？", "取消", "确定", R.style.MyDialog) {

                    @Override
                    protected void onYesClick() {

                        // TODO Auto-generated method stub
                        showPasswordPopupWindow(2);

                    }
                }.show();
                return true;
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_set_bank:
                show(AddBankCardActivity.class);
                break;
            case R.id.tv_public:
                if (TextUtils.isEmpty(tv_bankname.getText().toString().trim())) {
                    GetToast.useString(cnt, "请设置银行卡");
                    return;
                }

                if (TextUtils.isEmpty(et_money.getText().toString().trim())) {
                    GetToast.useString(cnt, "请填写充值金额");
                    return;
                }
                if (et_money.getText().toString().trim().equals("0")) {
                    GetToast.useString(cnt, "充值金额不能为0");
                    return;
                }
                if (!PublicMethod.formatDecimal(et_money.getText().toString().trim())) {
                    GetToast.useString(cnt, "数字格式错误");
                    return;
                }
                showPasswordPopupWindow(1);
                break;
        }
    }

    private void setBankView(int type, String bank, String number) {
        if (type == 1) {
            ll_set_bank.setVisibility(View.GONE);
            ll_bank.setVisibility(View.VISIBLE);
            tv_bankname.setText(bank);
            tv_number.setText("尾号" + number);
        } else {
            ll_set_bank.setVisibility(View.VISIBLE);
            ll_bank.setVisibility(View.GONE);
        }
    }

    private void showPasswordPopupWindow(final int type) {
        popup = new PayParswordPopupWindow(this);
        popup.showAtLocation(findViewById(R.id.ll_container), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        popup.setOnInputFinishListener(new PayParswordPopupWindow.OnInputFinish() {

            @Override
            public void onfinish(List<String> list) {

                // TODO Auto-generated method stub
                StringBuffer sb = new StringBuffer();
                for (String item : list) {
                    sb.append(item);
                }
                if (type == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putString("bankname", tv_bankname.getText().toString().trim());
                    bundle.putString("banknum", num);
                    bundle.putString("money", et_money.getText().toString().trim());
                    show(RechargeResultActivity.class, bundle);
                } else if (type == 2) {
                    deleteBankCard(sb.toString());
                }

            }
        });
        popup.setOnLeftClick(new PayParswordPopupWindow.OnLeftClick() {
            @Override
            public void onLeft() {
                show(ChangePayPasswordActivity.class);
            }
        });
        popup.setOnRightClick(new PayParswordPopupWindow.OnRightClick() {
            @Override
            public void onRight() {
                show(LoginActivity.class, LoginActivity.TYPE_VERIFY);
            }
        });
    }

    private void deleteBankCard(String pwd) {
        new GetDeleteBankCard(AppContext.myPreferences.getUid(), MD5.Md5(MD5.Md5(pwd)), new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                super.onSuccess(toast, type, o);
                if ("1".equals(o)) {
                    setBankView(2, "", "");
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(cnt, toast);
            }
        }).execute(cnt);
    }

    private void chargeInfo() {
        new GetChargeInfo(AppContext.myPreferences.getUid(), new AsyCallBack<GetChargeInfo.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetChargeInfo.Info info) throws Exception {
                num = info.number;
                setBankView(info.type, info.banktype, info.number);

            }

        }).execute(cnt);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onRefresh = null;
        if (popup != null) popup.dismiss();
    }

    public interface OnRefresh {
        void onRefresh();
    }
}
